package com.naizhuai.ui.task.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.exception.ValidateException;
import com.naizhuai.ui.entity.Task.TaskChartData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author：lichenxi
 * @date 2018/6/30 15
 * email：525603977@qq.com
 * Fighting
 */
public class DataUpdateAdapter extends RecyclerView.Adapter<DataUpdateAdapter.ViewHolder> {

    private List<TaskChartData> mChartData;
    private boolean editenable = true;

    public void setEditenable(boolean editenable) {
        this.editenable = editenable;
    }

    public void setResult(List<TaskChartData> taskChartData) {
        mChartData = taskChartData;
        notifyDataSetChanged();
    }

    public List<TaskChartData> getChartData() throws ValidateException {
        if (mChartData == null) {
            throw new ValidateException("数据为空");
        }
        for (TaskChartData taskChartData : mChartData) {
            if (TextUtils.isEmpty(taskChartData.value)) {
                throw new ValidateException(taskChartData.weekend + "尚未填写");
            }
        }
        return mChartData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_task_chart_update, parent, false);
        return new ViewHolder(view, new MyCustomEditTextListener());
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TaskChartData taskChartData = mChartData.get(holder.getAdapterPosition());
//        holder.detial.setEnabled(editenable);
          holder.week.setText(taskChartData.weekend+"周");
          holder.value.setText(taskChartData.value);
          holder.mEditTextListener.updatePosition(holder.getAdapterPosition());
    }

    @Override
    public int getItemCount() {
        return mChartData != null ? mChartData.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.week)
        TextView week;
        @BindView(R.id.value)
        EditText value;
        MyCustomEditTextListener mEditTextListener;

        public ViewHolder(View itemView, MyCustomEditTextListener mEditTextListener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mEditTextListener = mEditTextListener;
            value.addTextChangedListener(mEditTextListener);
        }
    }

    private class MyCustomEditTextListener implements TextWatcher {
        private int position;

        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            mChartData.get(position).value = charSequence.toString();
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }
    }
}
